package com.ruoyi.web.controller.talentServerController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.TalResourceType;
import com.ruoyi.system.domain.TalTalentInfo;
import com.ruoyi.system.domain.dto.TalentStatisticsDTO;
import com.ruoyi.system.service.ITalResourceTypeService;
import com.ruoyi.system.service.ITalTalentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : anxinr
 * @date : 2023/4/19 17:36
 */
@Slf4j
@RestController
@RequestMapping("/system/tal")
public class TalStatisticsController {
    @Autowired
    private ITalTalentInfoService talentInfoService ;

    @Autowired
    private ITalResourceTypeService typeService ;


    @PostMapping("/getStatisticsInfo")
    public Map getStatisticsInfo(@RequestBody TalentStatisticsDTO dto){
        HashMap<String, Object> dataMap = new HashMap<>();
        LambdaQueryWrapper<TalTalentInfo> wrapper = new LambdaQueryWrapper<>();
        /**时间的条件*/
        if (!Objects.isNull(dto)){
            if (CollectionUtils.isNotEmpty(dto.getTime())){
                wrapper.between(TalTalentInfo::getCreateTime,dto.getTime().get(0),dto.getTime().get(1)) ;
            }
        }
        /**所有的人才申请*/
        List<TalTalentInfo> talentInfoList = talentInfoService.list(wrapper);
        /**所有的人才类型*/
        List<TalResourceType> list = typeService.list();

        Map<Long, TalResourceType> parentMap = list.stream().filter((t) -> (t.getParentId() == 0L)).collect(Collectors.toMap(TalResourceType::getId, (t) -> (t)));
        Map<Long, TalResourceType> childMap = list.stream().filter((t) -> (t.getParentId() != 0L)).collect(Collectors.toMap(TalResourceType::getId, (t) -> (t)));


        Collection<TalResourceType> values = parentMap.values();
        for (TalResourceType value : values) {

            /**人才学历数据*/
            if ("学历".equals(value.getName())){
                List<TalResourceType> collect = childMap.values().stream().filter((child) -> (child.getParentId().equals(value.getId()))).collect(Collectors.toList());
                ArrayList<String> arrayList = new ArrayList<>();
                for (TalResourceType type : collect) {
                    arrayList.add(type.getName()) ;
                }
                dataMap.put("educationTitle",arrayList) ;
                ArrayList<String> strings = new ArrayList<>();
                for (TalResourceType type : collect) {
                    strings.add(talentInfoList.stream().filter((t)->(t.getLevelQualification().equals(type.getName()))).count() + "") ;
                }
                dataMap.put("educationData",strings) ;
            }
            /**人才专业数据*/

            if ("专业".equals(value.getName())){
                List<TalResourceType> collect = childMap.values().stream().filter((child) -> (child.getParentId().equals(value.getId()))).collect(Collectors.toList());
                ArrayList<String> arrayList = new ArrayList<>();
                for (TalResourceType type : collect) {
                    arrayList.add(type.getName()) ;
                }
                dataMap.put("specialityTitle",arrayList) ;
                ArrayList<String> strings = new ArrayList<>();
                for (TalResourceType type : collect) {
                    strings.add(talentInfoList.stream().filter((t)->(t.getLevelMajor().equals(type.getName()))).count() + "") ;
                }
                dataMap.put("specialityData",strings) ;
            }


        }


        return dataMap ;
    }
}
